package com.danceforyou.common.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpClient {

    public static String doGet(String url, List<NameValuePair> paramList) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = null;
        try {
            URIBuilder uri = new URIBuilder(url);
            //设置请求参数
            uri.setParameters(paramList);
            HttpGet httpGet = new HttpGet(uri.build());
            //设置请求状态参数
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(3000)
                    .setSocketTimeout(3000)
                    .setConnectTimeout(3000)
                    .build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();//获取返回状态值
            if (statusCode == HttpStatus.SC_OK) {//请求成功
                HttpEntity httpEntity = response.getEntity();
                if (httpEntity != null) {
                    result = EntityUtils.toString(httpEntity, "UTF-8");
                    EntityUtils.consume(httpEntity);//关闭资源
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = null;
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        List<NameValuePair> paramList = new ArrayList<NameValuePair>() {{
            add(new BasicNameValuePair("appid", "wx40257052242dc06d"));
            add(new BasicNameValuePair("secret", "5998c6d496e1fa557e148624c98d5dc4"));
            add(new BasicNameValuePair("js_code", "043qcl2P1ezZL61RcR1P1rwm2P1qcl2e"));
            add(new BasicNameValuePair("grant_type", "authorization_code"));
        }};
        String result = doGet(url, paramList);
        System.out.println(result);
    }
}
